<?php
/**
 * Created by PhpStorm.
 * Date: 2023/5/06
 * Time: 11:53
 */

namespace App\common\models;


use App\common\models\BaseModel;
use Illuminate\Support\Facades\DB;

/**
 * Class MemberModel
 * @package app\common\models
 */
class Member extends BaseModel
{
    const table = 'members';

    static $_field = ['*'];
//    static $_field = ['uid','avatar','nickname','mobile','email'];
    const _field = ['uid', 'avatar', 'nickname', 'mobile', 'email'];

    static $_instance; //本类的实例

    public static function getInstance()
    {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * 登录密码验证
     * @param $password
     * @return false|int
     */
    public function passwordValidate($password)
    {
        //规则 6-16位 数字，字母组合
        $pre_str = '/(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8,16}]/';
        $pre_res = preg_match($pre_str, $password);
        return $pre_res;
    }

    /**
     * 支付密码验证
     * @param $password
     * @return false|int
     */
    public function payPasswordValidate($password)
    {
        //规则 6位 数字
        $pre_str = '/^\d{6}$/';
        $pre_res = preg_match($pre_str, $password);
        return $pre_res;
    }

    /**
     * 密码加密规则
     * @param $password
     * @param $salt
     * @return string
     */
    public function passWordEncrypt($password, $salt)
    {
        //加密
        $password_encrypt = md5($password . $salt);

        return $password_encrypt;
    }

    /**
     * 获取用户信息
     * @param $member_id
     * @return array
     */
    public function getMember($member_id)
    {
        $info = [];
        if (empty($member_id)) {
            return $info;
        }
        $select = ['uid', 'gender', 'nickname', 'avatar', 'mobile', 'realname', 'address', 'createtime',];
        $info = self::select($select)->where('uid', $member_id)->first()->toarray();
        return $info;
    }

    /**
     * 根据条件，获取用户信息
     * @param $condition
     * @param array $fields
     * @return array
     */
    public function getMemberInfo($condition,array $fields = self::_field)
    {
        $info = [];
        if (empty($condition)) {
            return $info;
        }
        $info = self::select($fields)->where($condition)->first()->toarray();
        return $info;
    }

    /**
     * 获取用户列表
     * @param array $condition
     * @param array $condition_in
     * @param array $fields
     * @return array
     */
    public function getMemberList(array $condition = [], array $condition_in = [], array $fields = self::_field)
    {
        $query = DB::table(self::table)->select($fields)->where($condition);
        //查询uid
        if (!empty($condition_in)) {
            $query->whereIn('uid', $condition_in);
        }
        $data = $query->get()->toArray();
        return $data;
    }

    /**
     * 保存
     * @param $condition
     * @param $data
     * @return int|void
     */
    public function modifySave($condition, $data)
    {
        $data = DB::table(self::table)->where($condition)->update($data);
        return $data;
    }


}
